use crate::business::app_state::AppState;
use crate::business::websocket::dto::common_dto::{ExceptionResp, OkResp};
use crate::business::websocket::dto::im_group_dto::{GroupAddReq, GroupCreateReq, GroupInfoItem, GroupInfoReq};
use crate::business::websocket::send_data_handle::send_data_async;
use crate::business::websocket::send_data_router::{RpcRequest, RpcResponse};

pub struct RpcGroupService;

impl RpcGroupService {

    pub async fn group_create(app_state: AppState, group_create_req: GroupCreateReq) -> Result<OkResp, ExceptionResp> {
        let response = send_data_async(app_state.clone(), RpcRequest::GroupCreate(group_create_req)).await;
        if let RpcResponse::GroupCreateResponse(result) = response {
            return result;
        }

        Err(ExceptionResp::new("缺少对应的处理器".to_string()))
    }

    pub async fn group_add(app_state: AppState, group_add_req: GroupAddReq) -> Result<OkResp, ExceptionResp> {
        let response = send_data_async(app_state.clone(), RpcRequest::GroupAdd(group_add_req)).await;
        if let RpcResponse::GroupAddResponse(result) = response {
            return result;
        }

        Err(ExceptionResp::new("缺少对应的处理器".to_string()))
    }

    pub async fn pull_group_info(app_state: AppState, group_info_req: GroupInfoReq) -> Result<GroupInfoItem, ExceptionResp> {
        let response = send_data_async(app_state.clone(), RpcRequest::GroupPullInfo(group_info_req)).await;
        if let RpcResponse::GroupPullInfoResponse(result) = response {
            return result;
        }

        Err(ExceptionResp::new("缺少对应的处理器".to_string()))
    }
}